//
//  MessageViewController.m
//  Lovely Food
//
//  Created by 李经纬 on 15/12/9.
//  Copyright © 2015年 李经纬. All rights reserved.
//

#import "MessageViewController.h"
#import <AVOSCloud.h>

@interface MessageViewController ()
@property (weak, nonatomic) IBOutlet UITextView *messageTextView;

@property (nonatomic, strong) AVUser *currentUser;

@end

@implementation MessageViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:(UIBarButtonItemStyleDone) target:self action:@selector(rightBarButtonAction:)];
    // 设置调整textView在有navigation状态下文字不居左上角显示问题
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.messageTextView.layer.borderColor = [[UIColor grayColor] CGColor];
    self.messageTextView.layer.borderWidth = 2;
    
    self.currentUser = [AVUser currentUser];
}

- (void)rightBarButtonAction:(UIBarButtonItem *)sender
{

    AVObject *post = [AVObject objectWithClassName:@"Yan31Message"];
    [post setObject:self.messageTextView.text forKey:@"content"];
    [post setObject:self.currentUser.username forKey:@"pubUser"];
    [post setObject:self.currentUser.objectId forKey:@"pubObjectId"];
    
    NSDate *newDate = [NSDate date];
    NSLog(@"%@",newDate);
    [post setObject:newDate forKey:@"pubTimestamp"];
    
    
    [post saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSLog(@"成功");
            self.result();
            [self.navigationController popViewControllerAnimated:YES];
            
            
            
            
//            [self.currentUser setObject:post forKey:@"UserMessage"];
//            [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
//                if (succeeded) {
//                    NSLog(@"保存成功");
//                    
//                } else {
//                    NSLog(@"%@", error);
//                }
//                }];
         
        }
        
    }];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
